﻿package mortal.game.view.rebate.openServerSevenDay
{
    import Message.Public.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.rebate.data.*;
    import mortal.game.view.rebate.openNewSerSevenDay.*;
    import mortal.game.view.rebate.openNewSerSevenDay.active2.*;
    import mortal.game.view.rebate.openNewSerSevenDay.items.*;
    import mortal.game.view.rebate.openNewSerSevenDay.panels.*;
    import mortal.game.view.rebate.openServerSevenDay.data.*;
    import mortal.game.view.rebate.openServerSevenDay.item.*;
    import mortal.game.view.rebate.rank.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class OpenServerSevenDayController extends Controller
    {
        private var _sevenMNew:OpenNewSerSevenDayModule;
        private var _dataType:int;
        private var _isAdd:Boolean = false;
        private var _rangT:int = 0;

        public function OpenServerSevenDayController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.RebateOpenServerSevenDayModule, this.onOpenSevenModule, true);
            EventUtil.addSelfEvent(EventName.RebateOpenServerSevenDayModuleClose, this.onCloseSevenModule, true);
            EventUtil.addSelfEvent(EventName.RebateOpenServerSevenDayReq, this.getRangDataReq, true);
            EventUtil.addNetEvent(ServerCommand.RankingGetRankListSucc, this.onDataLoad, true);
            EventUtil.addNetEvent(ServerCommand.OpenServerSevenDayCheckCanGet, this.onCheckCanGet, true);
            EventUtil.addSelfEvent(EventName.RebateOpenServerSevenDayGetReward2, this.getSingleRewar2, true);
            EventUtil.addNetEvent(ServerCommand.OpenServerSevenDayGotReward2, this.getRewardBack2, true);
            EventUtil.addSelfEvent(EventName.RebateOpenServerSevenDayGetReward, this.getSingleRewar, true);
            EventUtil.addNetEvent(ServerCommand.OpenServerSevenDayGotReward, this.getRewardBack, true);
            EventUtil.addSelfEvent(EventName.RebateOpenSerSenRC200, this.onOpenRC200Hd, true);
            EventUtil.addNetEvent(ServerCommand.OpenNewServerSevenConsumInfoRes, this.onConsumInfoBack, true);
            EventUtil.addSelfEvent(EventName.RebateOpenSerSenRC200GetRw, this.getConsumRwHd, true);
            LevelCheckUtil.addInit(this.initHd);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._sevenMNew == null)
            {
                this._sevenMNew = new OpenNewSerSevenDayModule();
                this._sevenMNew.addEventListener(WindowEvent.SHOW, this.onShow);
                this._sevenMNew.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._sevenMNew;
        }// end function

        public function onShow(event:Event) : void
        {
            if (this._isAdd)
            {
                return;
            }
            this._isAdd = true;
            this.addEvents(true);
            return;
        }// end function

        public function onClose(event:Event) : void
        {
            if (this._isAdd == false)
            {
                return;
            }
            this._isAdd = false;
            this._dataType = -1;
            this.addEvents(false);
            if (OpenNewSerSevRight.isViewShow())
            {
                OpenNewSerSevRight.instance.hide();
            }
            return;
        }// end function

        private function addEvents(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.RebateSevenDayRankGetData, this.onGetRankData, param1);
            EventUtil.addNetEvent(ServerCommand.OpenServerSevenDayRankPanelGetData, this.onRankPanelDataBk, param1);
            return;
        }// end function

        private function onOpenSevenModule(param1:Object) : void
        {
            if (OpenServerSevenDayUtil.isNotOpenSpeActive())
            {
                return;
            }
            if (Cache.instance.rebate.oneSevenType != 0)
            {
                this.initView();
                this._sevenMNew.show();
            }
            else if (Cache.instance.rebate.comOtherType != 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.RebateDayTargetOpenModule));
            }
            return;
        }// end function

        private function onCloseSevenModule(param1:Object) : void
        {
            if (this._sevenMNew)
            {
                this._sevenMNew.hide();
            }
            return;
        }// end function

        private function getRangDataReq(event:DataEvent) : void
        {
            var _loc_2:* = getTimer();
            if (_loc_2 - this._rangT < 300)
            {
                return;
            }
            this._rangT = _loc_2;
            var _loc_3:* = event.data as int;
            var _loc_4:* = Cache.instance.openServerSevenDay.sevenDayTabArr[_loc_3];
            if (_loc_4 == null)
            {
                return;
            }
            this._dataType = int(Cache.instance.openServerSevenDay.getRangeTypeTyDayNum(_loc_4.dayNum));
            if (OpenServerSevenDayUtil.isIndexDateOff(_loc_4.dayNum))
            {
                GameProxy.ranking.getToplist(this._dataType, -1);
            }
            else
            {
                GameProxy.ranking.getToplist(this._dataType);
            }
            this.checkGanGetReward();
            return;
        }// end function

        private function onDataLoad(param1:Object) : void
        {
            var _loc_2:* = param1;
            var _loc_3:* = int(_loc_2.toplistType);
            var _loc_4:* = _loc_2.rankingList as Array;
            if (this._dataType != _loc_3)
            {
                return;
            }
            if (this._sevenMNew && this._sevenMNew.isHide == false)
            {
                this._sevenMNew.setRangeData(_loc_4, _loc_3);
            }
            return;
        }// end function

        private function checkGanGetReward() : void
        {
            var _loc_1:* = RebateUtil.getTargetType();
            if (ETargetType._ETargetTypeUNKNOWN == _loc_1)
            {
                return;
            }
            GameProxy.openServerSevenDay.getIfCanTakeSevenDaysTargetAward(_loc_1);
            return;
        }// end function

        private function onCheckCanGet(param1:Object) : void
        {
            if (this._sevenMNew && view)
            {
                this._sevenMNew.updateCanGet();
            }
            return;
        }// end function

        private function getSingleRewar(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = RebateUtil.getSevenDayRewardId();
            GameProxy.openServerSevenDay.takeTargetAwardById(_loc_3);
            return;
        }// end function

        private function getSingleRewar2(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = int(_loc_2);
            GameProxy.openServerSevenDay.takeTargetAwardById(_loc_3);
            return;
        }// end function

        private function getRewardBack(param1:Object) : void
        {
            var _loc_2:* = int(param1["t_id"]);
            if (this._sevenMNew && view)
            {
                this._sevenMNew.updateCanGet();
                MsgManager.showRollTipsMsg("" + Language.getString(71891));
            }
            if (OpenServerSevenIcon.hasInstance())
            {
                if (GameController.rebate.hasSevenDayRe())
                {
                    OpenServerSevenIcon.instance.playEffect();
                }
                else
                {
                    OpenServerSevenIcon.instance.stopEffect();
                }
            }
            if (OpenNewSerSevenDayMod2.isView)
            {
                OpenNewSerSevenDayMod2.instance.updateBtns();
            }
            return;
        }// end function

        private function getRewardBack2(param1:Object) : void
        {
            var _loc_2:* = int(param1["t_id"]);
            if (this._sevenMNew && view)
            {
                this._sevenMNew.updateCanGet();
                MsgManager.showRollTipsMsg("" + Language.getString(71892));
            }
            if (OpenServerSevenIcon.hasInstance())
            {
                if (GameController.rebate.hasSevenDayRe())
                {
                    OpenServerSevenIcon.instance.playEffect();
                }
                else
                {
                    OpenServerSevenIcon.instance.stopEffect();
                }
            }
            return;
        }// end function

        private function onGetRankData(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            var _loc_3:* = Cache.instance.openServerSevenDay.sevenDayRangeTabArr;
            var _loc_4:* = _loc_3[_loc_2];
            if (_loc_4 == null)
            {
                return;
            }
            this._dataType = int(Cache.instance.openServerSevenDay.getRangeTypeTyDayNum(_loc_4.dayNum));
            if (OpenServerSevenDayUtil.isIndexDateOff(_loc_4.dayNum))
            {
                GameProxy.openServerSevenDay.getRankToplistDateOff(this._dataType);
            }
            else
            {
                GameProxy.openServerSevenDay.getRankToplist(this._dataType);
            }
            return;
        }// end function

        private function onRankPanelDataBk(param1:Object) : void
        {
            if (RebateRankPanel.hasInstance && !RebateRankPanel.instance.isHide)
            {
                RebateRankPanel.instance.updateDate();
            }
            return;
        }// end function

        private function onOpenRC200Hd(event:DataEvent) : void
        {
            OpenServerSevDayRC200.instance.show();
            return;
        }// end function

        private function onConsumInfoBack(param1:Object) : void
        {
            if (Cache.instance.openServerSevenDay.consumRewardInfo && RebateUtil.getTodayConsum() != 0)
            {
                if (OpenServerSevenDayUtil.isNotOpenSpeActive())
                {
                    return;
                }
                OpenServerSevDayRC200Icon.instance.show();
                if (RebateUtil.hasConsumRw())
                {
                    OpenServerSevDayRC200Icon.instance.playEffect();
                }
                else
                {
                    OpenServerSevDayRC200Icon.instance.stopEffect();
                }
            }
            else if (OpenServerSevDayRC200Icon.hasInstance() && OpenServerSevDayRC200Icon.instance.isHide == false)
            {
                OpenServerSevDayRC200Icon.instance.stopEffect();
                OpenServerSevDayRC200Icon.instance.hide();
            }
            if (OpenServerSevDayRC200.isView())
            {
                OpenServerSevDayRC200.instance.updateInfo();
            }
            return;
        }// end function

        private function getConsumRwHd(event:DataEvent) : void
        {
            if (RebateUtil.hasConsumRw())
            {
                GameProxy.role.getConsumReward();
            }
            else
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71893));
            }
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            this.onConsumInfoBack(null);
            return;
        }// end function

    }
}
